package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.hiddendanger.track.RectificationPlan;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionStation;
import com.sduept.bigdata.equipment.assessment.service.InspectionPlanService;
import com.sduept.bigdata.equipment.assessment.service.RectificationPlanService;
import com.sduept.bigdata.equipment.assessment.service.impl.DeductionTaskService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.entity.AbstractStandardDict;
import com.sduept.core.service.StandardDictService;
import com.sduept.core.service.impl.StandardDictConfigurator;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.DepartManager;
import com.sduept.permission.manager.PersonnelManager;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.*;

/**
 * 整改计划 详情查看/编辑controller类
 */
@Named
@ViewScoped
public class RectificationPlanDetailController extends AbstractController {

	private static final long serialVersionUID = 9142633244458603467L;
	
	@Autowired
	private RectificationPlanService service;
	@Autowired
	private StandardDictService sman;
	@Autowired
	private StandardDictConfigurator sdc;
	@Autowired
	private DeductionTaskService deductService;
	@Autowired
	private InspectionPlanService planService;
	@Autowired
	private DepartManager dm;
	@Autowired
	private PersonnelManager pm;
	
	/**
	 * true:编辑功能
	 * false:查看详细功能
	 */
	private boolean status;
	private String isFinished = "全部"; // 是否完成
	private RectificationPlan current = null;
	private Map<String, List<String>> discMap = null;// 字典map
	private Class<? extends AbstractStandardDict> T = null;

	/**
	 * 整改人员选择范围
	 */
	private List<PersonnelEntityBean> rectificationPersons = null;
	
	@PostConstruct
	public void init() {
		initDicsMap();
		initStatus(getParameter("status"));
		try {
			current = service.findById(getParameter("rectificationPlanId"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		rectificationPersons = getRectificationPerson(current);
	}

	private void initStatus(String strStatus) {
		if ("true".equals(strStatus)) {
			status = true;
		} else {
			status = false;
		}
	}
	
	/**
	 * 初始化字典表
	 */
	@SuppressWarnings("unchecked")
	private void initDicsMap() {
		discMap = new HashMap<String, List<String>>();
		List<String> dicts = Arrays.asList("HiddenDangerQuesType","HiddenDangerReasonType","HiddenDangerLevel"	,"HiddenDangerSource");
		try {
			for (String className : dicts) {
				T=sdc.getT(className);
				List<AbstractStandardDict> dictList = (List<AbstractStandardDict>) sman.findAll(Class.forName(T.getName()));
				List<String> values = new ArrayList<String>();
				for (AbstractStandardDict d : dictList) {
					values.add(d.getName());
				}
				discMap.put(className, values);
			}
		} catch (Exception e) {
			addErrorMessage("字典库加载失败");
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取整改人员可选列表
	 * 互查计划：被检单位下的人员
	 * 自查计划：检查单位下的人员
	 * @param planId
	 * @return 
	 */
	private List<PersonnelEntityBean> getRectificationPerson(RectificationPlan rplan) {
		if(null == rplan) {
			return new ArrayList<PersonnelEntityBean>();
		}
		InspectionStation inspectionStation = deductService.getInspectionStationById(rplan.getInspectionStationId());
		String dept = inspectionStation.getAcceptUnit();
		// 根据dept获取人员
		List<DepartmentEntityBean> depts = dm.findDepartmentsByName(dept);
		List<PersonnelEntityBean> persons = new ArrayList<PersonnelEntityBean>();
		for (DepartmentEntityBean d : depts) {
			persons.addAll(dm.findDeptPersonnels(d.getId()));
		}
		return persons;
	}
	
	/**
	 * 整改人员选择
	 */
	public List<PersonnelEntityBean> completeRectificationPerson(String query) {
		List<PersonnelEntityBean> results = new ArrayList<PersonnelEntityBean>();
		for (PersonnelEntityBean p : rectificationPersons) {
			if (p.getName().contains(query)) {
				results.add(p);
			}
		}
		return results;
	}
	
	/**
	 * 选择整改人员后，自动填充联系方式和code
	 * @param event
	 */
	public void selectedPerson(SelectEvent event) {
		String pId = (String) event.getObject();
		PersonnelEntityBean p = pm.findPersonnelById(pId);
		if(null != p) {
			current.setRectificationPersonCode(p.getCode());
			current.setRectificationPersonphone(p.getTelephone());
		}
	}

	/**
	 * 问题归类
	 * @param query
	 * @return
	 * @throws Exception 
	 */
	public List<String> completeHiddenDangerQuesClass(String query) throws Exception {
		List<String> results = new ArrayList<String>();
		List<String> list = discMap.get("HiddenDangerQuesType");
		for (String s : list) {
			if (s.contains(query)) {
				results.add(s);
			}
		}
		return results;
	}
	
	public void saveUpdate() {
		if(null != current) {
			service.createOrUpdate(current);
			addSuccessMessage("保存成功！");
		}
		current = null;
	}
	
	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String getIsFinished() {
		return isFinished;
	}

	public void setIsFinished(String isFinished) {
		this.isFinished = isFinished;
	}

	public RectificationPlan getCurrentTrack() {
		return current;
	}

	public void setCurrentTrack(RectificationPlan currentTrack) {
		this.current = currentTrack;
	}

}
